package com.sfh.cpc.home.menu;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sfh.cpc.R;
import com.sfh.cpc.com.entity.EMass;
import com.sfh.cpc.com.entity.RspPage;
import com.sfh.cpc.com.refresh.SwipeRefreshMoreLayout;
import com.sfh.cpc.com.widget.WidgetBaseToolbar;
import com.sfh.cpc.mass.MassActivity;
import com.sfh.cpc.mass.adapter.MassAdapter;
import com.sfh.cpc.mass.model.MassModel;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;
import com.sfh.lib.ui.AbstractLifecycleFragment;

/**
 * 功能描述: TODO
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/6
 */
public class MassFragment extends AbstractLifecycleFragment<MassModel> implements SwipeRefreshMoreLayout.OnLoadingListener, BaseQuickAdapter.OnItemClickListener {
    WidgetBaseToolbar toolbar;
    SwipeRefreshMoreLayout layout;
    MassAdapter adapter;
    int nowPage = 1;

    String type ="1";

    TextView tv_zh;
    TextView tv_new;
    TextView tv_vip;

    TextView tv_title;

    @Override
    public int getLayout() {
        return R.layout.mass_fragment;
    }
    int height = 100;
    int overallXScroll = 0;

    @Override
    public void initData(View view) {
       View top =  View.inflate(getContext(), R.layout.mass_top_view,null);
        tv_vip = top.findViewById(R.id.tv_vip);
        tv_zh = top.findViewById(R.id.tv_zh);
        tv_new  = top.findViewById(R.id.tv_new);

        tv_title = view.findViewById(R.id.tv_title);
        toolbar = view.findViewById(R.id.toolbar);
        toolbar.getIvBack().setVisibility(View.GONE);
        toolbar.setTitle("社团");
        toolbar.addLine();

        layout = view.findViewById(R.id.swipeRefreshMoreLayout);
        adapter = new MassAdapter();
        layout.setAdapter(adapter, true);
        adapter.addHeaderView(top);

        layout.setOnItemClickListener(this);
        layout.setOnLoadingListener(this);
        layout.onAutoRefresh();

        tv_title.setText("综合");
        tv_zh.setSelected(true);
        tv_zh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_zh.setSelected(true);
                tv_vip.setSelected(false);
                tv_new.setSelected(false);
                tv_title.setText("综合");
                type ="1";
                layout.onAutoRefresh();
            }
        });
        tv_new.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_zh.setSelected(false);
                tv_vip.setSelected(false);
                tv_new.setSelected(true);
                tv_title.setText("实时");
                type ="2";
                layout.onAutoRefresh();
            }
        });

        tv_vip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_vip.setSelected(true);
                tv_zh.setSelected(false);
                tv_new.setSelected(false);
                tv_title.setText("私密");
                type ="3";
                layout.onAutoRefresh();
            }
        });

        this.layout.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                //dy > 0 时为手指向上滚动,列表滚动显示下面的内容
                //dy < 0 时为手指向下滚动,列表滚动显示上面的内容
                // 累加y值 解决滑动一半y值为0
                overallXScroll = overallXScroll + dy;
                if (overallXScroll <= 0) {
                    //设置标题的背景颜色
                    setalpha(0);

                } else if (overallXScroll > 0 && overallXScroll <= height) {
                    //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
                    float scale = (float) overallXScroll / height;
                    int alpha = (int) (255 * scale);
                    setalpha(alpha);
                } else {
                    setalpha(255);
                }
            }
        });
    }
    private void setalpha(int alpha) {

        int argb = Color.argb(alpha, 255, 255, 255);
        this.tv_title.setBackgroundColor(argb);
        this.tv_title.setVisibility(alpha<=0? View.GONE:View.VISIBLE);
    }
    @Override
    public void onRefreshUI() {
        nowPage = 1;
        this.getViewModel().pageMass(nowPage,type);
    }

    @Override
    public void onLoadMoreUI() {
        nowPage++;
        this.getViewModel().pageMass(nowPage,type);
    }

    @LiveDataMatch
    public void onPageSuccess(RspPage<EMass> data) {

        if (data == null) {
            this.layout.onLoadMoreFail();

        } else {
            nowPage = data.getPageNumber();
            layout.onRefreshCompale(data.getList(), data.getPageNumber() != 1);
            if (data.getTotalPage() == data.getPageNumber()) {
                layout.onLoadAllCompale();
            }
        }

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        EMass mass = (EMass) adapter.getItem(position);
        MassActivity.startUI(getActivity(), mass);
    }
}
